// 处理港口数据

/**
 * 获取港口名称
 * @param {*} list  港口数据
 * @param {*} id 港口Id
 * @returns String  美国 纽约港口
 */
export const getPortName = (list, id) => {
	const item = list.find((x) => {
		return x.id === id;
	});
	if (!item) return ''
	return `${item.country} ${item.city} ${item.harbor}`
}


/**
 * 获取港口数据
 * @param {*} list  港口数据
 * @returns [] 返回 [美国，纽约，港口]
 */
export const getPortModels = (list) => {
	const newArray = [];
	const newArray2 = [];
	list.forEach((item) => {
		const repeat = newArray.find((res) => res.label === item.country);
		if (!repeat) {
			newArray.push({
				value: item.country,
				label: item.country,
				children: [item],
			});
		} else {
			repeat.children.push(item);
		}
	});

	newArray.forEach((item) => {
		item.children.forEach((i) => {
			const repeat = newArray2.find((res) => res.label === i.city);
			if (!repeat) {
				newArray2.push({
					value: i.city,
					label: i.city,
					country: i.country,
					children: [{
						value: i.id,
						label: i.harbor,
					}, ],
				});
			} else {
				repeat.children.push({
					value: i.id,
					label: i.harbor,
				});
			}
		});
	});
	newArray.forEach((item) => {
		item.children.length = 0;
		newArray2.forEach((i) => {
			if (item.label == i.country) {
				item.children.push({
					value: i.value,
					label: i.label,
					children: i.children,
				});
			}
		});
	});
	return newArray;
};

/**
 * 获车辆数据
 * @param {*} list  车辆数据
 * @returns [] 返回 [brand,series,model]
 */
export const getCarModels = (list) => {
	const newArray = [];
	const newArray2 = [];
	list.forEach((item) => {
		const repeat = newArray.find((res) => res.label === item.brand);
		if (!repeat) {
			newArray.push({
				value: item.brand,
				label: item.brand,
				children: [item],
			});
		} else {
			repeat.children.push(item);
		}
	});

	newArray.forEach((item) => {
		item.children.forEach((i) => {
			const repeat = newArray2.find((res) => res.label === i.series);
			if (!repeat) {
				newArray2.push({
					value: i.series,
					label: i.series,
					brand: i.brand,
					children: [{
						value: i.id,
						label: i.model,
					}, ],
				});
			} else {
				repeat.children.push({
					value: i.id,
					label: i.model,
				});
			}
		});
	});
	newArray.forEach((item) => {
		item.children.length = 0;
		newArray2.forEach((i) => {
			if (item.label == i.brand) {
				item.children.push({
					value: i.value,
					label: i.label,
					children: i.children,
				});
			}
		});
	});
	return newArray;
};